package com.modificial.shiro.system.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.modificial.shiro.system.dao.RoleDao;
import com.modificial.shiro.system.dao.RolePermissionDao;
import com.modificial.shiro.system.dao.UserRoleDao;
import com.modificial.shiro.system.domain.SysRole;
import com.modificial.shiro.system.domain.SysRolePermission;
import com.modificial.shiro.system.exception.ServiceException;
import com.modificial.shiro.system.service.RoleService;
import com.modificial.shiro.util.Page;
import com.modificial.shiro.util.SnowFlakeGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author modificial
 * @date 2018/8/5 0005
 * @company modificial_org
 * @description 角色管理
 */
@Service
@Transactional(isolation = Isolation.READ_COMMITTED, rollbackFor = Exception.class)
public class RoleServiceimpl implements RoleService {

    @Autowired
    private RoleDao roleDao;

    @Autowired
    private RolePermissionDao rolePermissionDao;

    @Autowired
    private UserRoleDao userRoleDao;

    @Override
    public int addRole(SysRole sysRole) {
        Assert.notNull(sysRole,"角色不能为空");
        sysRole.setUpdateTime(new Date());
        sysRole.setIsEnable(1);
        sysRole.setUpdateTime(new Date());
        sysRole.setRoleId(SnowFlakeGenerator.nextId());
       return roleDao.insert(sysRole);
    }

    @Override
    public int updateRole(SysRole sysRole) {
        Assert.notNull(sysRole,"角色不能为空");
        return roleDao.updateRole(sysRole);
    }

    @Override
    public int disableRole(String roleId, Integer isEnable) {
        Assert.isTrue(StringUtils.hasText(roleId)&&isEnable!=null,"角色id不能为空");
        SysRole sysRole=roleDao.selectByPrimaryKey(roleId);
        int status=sysRole.getIsEnable();
        if(status==isEnable){
            throw new ServiceException("角色已经被禁用或启用");
        }

        return roleDao.disableRole(roleId,isEnable);
    }

    @Override
    public Page<SysRole> listRoles(Integer nowPage, Integer pageSize) {
        PageHelper.startPage(nowPage,pageSize);
        List<SysRole>sysRoles=roleDao.selectAll();
        PageInfo<SysRole>pageInfo=new PageInfo<>(sysRoles);
        Page<SysRole>page=new Page<>(pageInfo.getTotal(),pageInfo.getList(),nowPage,pageSize,pageInfo.getPages());
        return page;
    }

    @Override
    public SysRole findRoleById(String roleId) {
        Assert.isTrue(StringUtils.hasText(roleId),"用户id不能为空");
        return roleDao.selectByPrimaryKey(roleId);
    }

    /**
     * 授权权限
     * @param roleId 角色id
     * @param permissions 权限集合
     * @return
     */
    @Override
    public int grantForRole(String roleId, String permissions){
        Assert.isTrue(StringUtils.hasText(roleId),"角色id不能为空");
        rolePermissionDao.deletePermissionByRole(roleId);
        String[] permissionIds=permissions.split(",");
        List<String>list=Arrays.asList(permissionIds);
        list.forEach(it->{
            SysRolePermission sysRolePermission=new SysRolePermission();
            sysRolePermission.setPermissionId(it);
            sysRolePermission.setRoleId(roleId);
            rolePermissionDao.insert(sysRolePermission);
        });
        return  1;
    }

    /**
     * 根据角色id删除角色
     * @param roleId 角色id
     * @return
     */
    @Override
    public int deleteRole(String roleId) {
        Assert.isTrue(StringUtils.hasText(roleId),"用户id不能为空");
        rolePermissionDao.deletePermissionByRole(roleId);
        userRoleDao.deleteUserByRole(roleId);
        roleDao.deleteByPrimaryKey(roleId);
       return 1;
    }

    /**
     * 根据角色删除用户关系
     * @param roleId 角色id
     * @return
     */
    @Override
    public int deleteUserByRole(String roleId) {
        Assert.isTrue(StringUtils.hasText(roleId),"角色id不能为空");
        return  userRoleDao.deleteUserByRole(roleId);
    }
}
